package org.example.redis_concurrent.service;

import org.example.redis_concurrent.utils.RandomUtils;
import org.example.redis_concurrent.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class AsyncService {

    public static final String HY_KEY="hyToken";

    @Resource
    private RedisUtils redisUtils;

    @Async
    public String  setHyToken()  {
        String token= RandomUtils.genRandString(32);
        log.info("进入setHyToken函数。{}",token);
        try {
            Thread.sleep(5*100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }
        synchronized (this) {
            redisUtils.set(HY_KEY, token);
        }
        log.info("设置redis key为{}，值为{}。",HY_KEY,token);
        return "success!";
    }

}
